org.eclipse.vtp.framework.util
Class XMLUtilities

java.lang.Object
  extended by org.eclipse.vtp.framework.util.XMLUtilities

public class XMLUtilities
extends java.lang.Object

Static utility class that hosts functions that make dealing with xml formatted content easier.

Author:
trip

Constructor Summary
XMLUtilities()
           
 
Method Summary
static java.lang.String encodeAttribute(java.lang.String toEncode)
          Encodes the given string so that it conforms to the XML 1.0 specification for attribute values.
static java.lang.String encodeText(java.lang.String toEncode)
          Encodes the given string so that it conforms to the XML 1.0 specification for CDATA and TEXT nodes.
static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
          Convenience function to get a DOM document builder object.
static org.w3c.dom.Element[] getElementsOfNodeList(org.w3c.dom.NodeList nodeList)
          Returns an array containing the element objects in the provided node list.
static java.lang.String getElementTextData(org.w3c.dom.Element element)
          Returns the text content of the provided element as is.
static java.lang.String getElementTextData(org.w3c.dom.Element element, boolean unindent)
          Returns the text content of the provided element.
static org.w3c.dom.NodeList getNamedNodeList(org.w3c.dom.Element parent, java.lang.String containerTagName)
          Retrieves the DOM NodeList contained by the named element in the given parent element.

static java.lang.String getWrappedTextData(org.w3c.dom.Element parent, java.lang.String childTagName)
          A convenience method that allows the extraction of text data from a named child element of the given parent element.

static org.w3c.dom.Document loadDocument(java.io.File documentFile)
          Loads the given file using the default XML DOM implementation.
static java.lang.String unindentTextData(java.lang.String text)
          Removes excess indention from the provided text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtilities

public XMLUtilities()
Method Detail

encodeAttribute

public static java.lang.String encodeAttribute(java.lang.String toEncode)
Encodes the given string so that it conforms to the XML 1.0 specification for attribute values.

Parameters:
toEncode - The string to encode
Returns:
the encoded string

encodeText

public static java.lang.String encodeText(java.lang.String toEncode)
Encodes the given string so that it conforms to the XML 1.0 specification for CDATA and TEXT nodes.

Parameters:
toEncode - The string to encode
Returns:
the encoded string

getNamedNodeList

public static org.w3c.dom.NodeList getNamedNodeList(org.w3c.dom.Element parent,
                                                    java.lang.String containerTagName)
                                             throws java.lang.Exception
Retrieves the DOM NodeList contained by the named element in the given parent element.

 
     
         
         
     
 
 
In the above example, a NodeList containing the two
elements would be returned by passing the parent element and "named-list" into the function parameters.

Parameters:
parent - The parent DOM element that contains the named list.
containerTagName - The name of the element that contains the list items.
Returns:
A NodeList containing the list items.
Throws:
java.lang.Exception - If an exception occurs while traversing the DOM objects.

getElementsOfNodeList

public static org.w3c.dom.Element[] getElementsOfNodeList(org.w3c.dom.NodeList nodeList)
Returns an array containing the element objects in the provided node list.

Parameters:
nodeList - The DOM node objects to extract elements from.
Returns:
The array of DOM elements found in the node list.

getWrappedTextData

public static java.lang.String getWrappedTextData(org.w3c.dom.Element parent,
                                                  java.lang.String childTagName)
                                           throws java.lang.Exception
A convenience method that allows the extraction of text data from a named child element of the given parent element.

 
     [Text Data]
 
 

Parameters:
parent - The parent DOM element.
childTagName - The name of the child element.
Returns:
The text contained by the named child element.
Throws:
java.lang.Exception - If an error occurs while traversing the DOM objects.

getElementTextData

public static java.lang.String getElementTextData(org.w3c.dom.Element element)
                                           throws java.lang.Exception
Returns the text content of the provided element as is. If multiple text DOM nodes are present, they are concatenated together.

Parameters:
element - The element that contains the desired text content.
Returns:
The text content of the given element.
Throws:
java.lang.Exception - If an exception occurs during the traversal of the DOM objects.

getElementTextData

public static java.lang.String getElementTextData(org.w3c.dom.Element element,
                                                  boolean unindent)
                                           throws java.lang.Exception
Returns the text content of the provided element. If unindent is false, the contents are returned as-is. Otherwise, indention performed for formatting sake will be removed.

Parameters:
element - The element that contains the text content.
unindent - Whether or not to unindent the text content.
Returns:
The text content of the provided element.
Throws:
java.lang.Exception - If an exception occurs during the traversal of the DOM elements.

unindentTextData

public static java.lang.String unindentTextData(java.lang.String text)
Removes excess indention from the provided text. Also trims blank lines from the beginning and end of the text.

Parameters:
text - The text to unindent.
Returns:
The reformatted text.

getDocumentBuilder

public static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
                                                            throws javax.xml.parsers.ParserConfigurationException
Convenience function to get a DOM document builder object.

Returns:
A new DocumentBuilder instance.
Throws:
javax.xml.parsers.ParserConfigurationException

loadDocument

public static org.w3c.dom.Document loadDocument(java.io.File documentFile)
                                         throws javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException,
                                                java.io.IOException
Loads the given file using the default XML DOM implementation.

Parameters:
documentFile - The file to load.
Returns:
The DOM document contained in the file.
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
java.io.IOException